<taglib name="Admin\TagLib\EasyUI" />

<easyui:datagrid options="datagrid.options" fields="datagrid.fields" />

<script type="text/javascript">
var adminMemberModule = {
	dialog:   '#globel-dialog-div',
	datagrid: '#admin_memberlist_datagrid',
	
	//工具栏
	toolbar: [
		{ text: '添加用户', iconCls: 'icons-table-table_add', handler: function(){adminMemberModule.add();}},
		{ text: '刷新', iconCls: 'icons-table-table_refresh', handler: function(){adminMemberModule.refresh();}}
	],
	
	//时间字段格式化
	time: function(val){
		return val != '1970-01-01 08:00:00' ? val : '';
	},
	
	//操作字段格式化
	operate: function(val,arr,index){
		
		var btn = [];
		
		if(typeof(arr.userid)=='undefined') return '-';
		
		
		if(arr.username == 'admin'){//超级管理员去除权限：编辑用户、重置密码、删除
			//btn.push('没有操作权限');
			//btn.push('编辑用户');
			//btn.push('重置密码');
			//btn.push('删除');
		}else if (arr.relationuid>0){//店长类管理员去除权限:删除、编辑用户、添加			
			btn.push('<a href="javascript:void(0);" onclick="adminMemberModule.pageToUserInfo('+arr.relationuid+')">查看资料</a>');
			//btn.push('编辑用户');			
			//btn.push('<a href="javascript:void(0);" onclick="adminMemberModule.password('+val+')">初始化密码</a>');
			//btn.push('删除');
		}else{
			btn.push('<a href="javascript:void(0);" onclick="adminMemberModule.edit('+val+')">编辑用户</a>');
			//添加初始化密码功能！！密码初始化规则:手机号后6位
			btn.push('<a href="javascript:void(0);" onclick="adminMemberModule.password('+val+')">初始化密码</a>');
			btn.push('<a href="javascript:void(0);" onclick="adminMemberModule.delete('+val+')">删除</a>');
		}
		
		/*if(val == 1){
			btn.push('编辑用户');
//			btn.push('重置密码');
			btn.push('删除');
		}else{
			btn.push('<a href="javascript:void(0);" onclick="adminMemberModule.edit('+val+')">编辑用户</a>');
//			btn.push('<a href="javascript:void(0);" onclick="adminMemberModule.password('+val+')">重置密码</a>');
			btn.push('<a href="javascript:void(0);" onclick="adminMemberModule.delete('+val+')">删除</a>');
		}*/
		return btn.join(' | ');
	},
	
	//刷新列表
	refresh: function(){
		$(this.datagrid).datagrid('reload');
	},
	
	//添加用户
	add: function(){
		var that = this;
		$(that.dialog).dialog({
			title: '添加用户',
			iconCls: 'icons-application-application_add',
			width: 370,
			height: 330,
			cache: false,
			href: '<{:U('admin/Admin/memberAdd')}>',
			modal: true,
			collapsible: false,
			minimizable: false,
			resizable: false,
			maximizable: false,
			buttons:[{
				text:'确定',
				iconCls:'icons-other-tick',
				handler: function(){
					$(that.dialog).find('form').eq(0).form('submit', {
						onSubmit: function(){
							var isValid = $(this).form('validate');
							if (!isValid) return false;
							
							$.messager.progress({text:'处理中，请稍候...'});
							$.post('<{:U('admin/Admin/memberAdd?dosubmit=1')}>', $(this).serialize(), function(res){
								$.messager.progress('close');
								if(!res.status){
									$.app.method.tip('提示信息', res.info, 'error');
								}else{
									$.app.method.tip('提示信息', res.info, 'info');
									$(that.dialog).dialog('close');
									that.refresh();
								}
							}, 'json');
							
							return false;
						}
					});
				}
			},{
				text:'取消',
				iconCls:'icons-arrow-cross',
				handler: function(){
					$(that.dialog).dialog('close');
				}
			}]
		});
	},
	
	//编辑用户
	edit: function(id){
		if(typeof(id) !== 'number'){
			$.app.method.tip('提示信息', '未选择用户', 'error');
			return false;
		}
		var href = '<{:U('admin/Admin/memberEdit')}>';
		href += href.indexOf('?') != -1 ? '&id='+id : '?id='+id;

		var that = this;
		$(that.dialog).dialog({
			title: '编辑用户',
			iconCls: 'icons-application-application_edit',
			width: 370,
			height: 330,
			cache: false,
			href: href,
			modal: true,
			collapsible: false,
			minimizable: false,
			resizable: false,
			maximizable: false,
			buttons:[{
				text:'确定',
				iconCls:'icons-other-tick',
				handler:function(){
					$(that.dialog).find('form').eq(0).form('submit', {
						onSubmit: function(){
							var isValid = $(this).form('validate');
							if (!isValid) return false;
							
							$.messager.progress({text:'处理中，请稍候...'});
							var action = '<{:U('admin/Admin/memberEdit', array('dosubmit'=>1))}>';
							action += action.indexOf('?') != -1 ? '&id='+id : '?id='+id;
							$.post(action, $(this).serialize(), function(res){
								$.messager.progress('close');
								if(!res.status){
									$.app.method.tip('提示信息', res.info, 'error');
								}else{
									$.app.method.tip('提示信息', res.info, 'info');
									$(that.dialog).dialog('close');
									that.refresh();
								}
							}, 'json');
							
							return false;
						}
					});
				}
			},{
				text: '取消',
				iconCls: 'icons-arrow-cross',
				handler: function(){
					$(that.dialog).dialog('close');
				}
			}]
		});
	},
	
	//删除用户
	delete: function(id){
		if(typeof(id) !== 'number'){
			$.app.method.tip('提示信息', '未选择用户', 'error');
			return false;
		}
		
		var that = this;
		$.messager.confirm('提示信息', '确定要删除吗？', function(result){
			if(!result) return false;
			
			$.messager.progress({text:'处理中，请稍候...'});
			$.post('<{:U('admin/Admin/memberDelete')}>', {id: id}, function(res){
				$.messager.progress('close');
				
				if(!res.status){
					$.app.method.tip('提示信息', res.info, 'error');
				}else{
					$.app.method.tip('提示信息', res.info, 'info');
					that.refresh();
				}
			}, 'json');
		});
	},
	//查看用户资料
	pageToUserInfo: function(relation_userid){
		
		if(typeof(relation_userid) !== 'number'){
			$.app.method.tip('提示信息', '未选择用户', 'error');
			return false;
		}	
		var that = this;
		
		
		href = '<{:U('admin/user/index')}>';
    	href += '?user_id='+relation_userid;
    	
		$(that.dialog).dialog({
			title: '用户资料',
			iconCls: 'icons-application-application_view_detail',
			width: 650,
			height: 450,
			cache: false,
			href: href,
			modal: true,
			collapsible: false,
			minimizable: false,
			resizable: true,
			maximizable: true,
			buttons:[{
				text: '关闭',
				iconCls: 'icons-arrow-cross',
				handler: function(){
					$(that.dialog).dialog('close');
				}
			}]
		});
		
		/*$('#admin_memberlist_datagrid').datagrid('selectRow',index);// 关键在这里  
		var row = $('#admin_memberlist_datagrid').datagrid('getSelected');
		if(row){

			href = '<{:U('admin/user/index')}>';
	    	href += '?submit=outerJoin';
	    	href += '&user_id='+row['relationuid'];
	    	baseModule.openUrl(href,"用户详情");
		}*/
		return false;
	},
	
	
	//重置密码
	password: function(id){
		if(typeof(id) !== 'number'){
			$.app.method.tip('提示信息', '未选择用户', 'error');
			return false;
		}
		
		var that = this;
		$.messager.confirm('提示信息', '确定要重置密码吗？', function(result){
			if(!result) return false;
			
			$.messager.progress({text:'处理中，请稍候...'});
			$.post("<{:U('admin/Admin/memberResetPassword')}>", {id: id}, function(res){
				$.messager.progress('close');
				
				if(!res.status){
					$.app.method.tip('提示信息', res.info, 'error');
				}else{
// 					$.app.method.tip('提示信息', res.info, 'info');
// 					$.messager.alert('提示信息', '密码已重置为：' + res.password + '，请牢记新密码！', 'info');
					$.messager.alert('提示信息',res.info,'info');
				}
			}, 'json');
		});
	}
}
</script>